package com.kuangstudy.controller.string.pv01;

import com.kuangstudy.service.pv.BbsPvService;
import com.kuangstudy.service.pv.BbsPvServiceConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/5/20 14:39
 */
@Controller
public class BbsTopicPvController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BbsPvService bbsPvService;

    @GetMapping("/bbs/detail/{id}")
    public String pvindex(@PathVariable("id") Integer id, ModelMap modelMap) {
        Long increment = redisTemplate.opsForValue().increment(BbsPvServiceConstants.CACHE_SECOND_PV_LIST + id);
        modelMap.put("increment", increment);
        return "pv01/index";
    }

    @GetMapping("/bbs/detail2/{id}")
    public String pvindex2(@PathVariable("id") Integer id, ModelMap modelMap) {
        // 模拟高并发处理请求
        bbsPvService.addBssPv(id);
        // 从缓存中去获取最新的请求
        Object increment2 = redisTemplate.opsForValue().get(BbsPvServiceConstants.CACHE_SECOND_PV_LIST + id);
        modelMap.put("increment", increment2==null?0:increment2);
        return "pv01/index";
    }
}
